import {
    setTokenKey,
    setRefreshTokenKey,
    setUsernameKey,
    logout,
    setDepartmentKey
} from "../Type";
import {
    getToken,
    getRefreshToken,
    getUsername,
    getDepartment
} from "@/utils/cookies"

// 全局配置
const app = {
    token: "" || getToken(), // 登录后存储了 token 和 username
    retoken: "" || getRefreshToken(),
    username: "" || getUsername(),
    department: "" || getDepartment(),
}

// config Reducer
const configReducer = function(state = app, action) {
    switch (action.type) {
        // token
        case setTokenKey:
            {
                return {
                    ...state,
                    token: action.value,
                }
            }
        case setRefreshTokenKey:
            {
                return {
                    ...state,
                    retoken: action.value,
                }
            }
        // username
        case setUsernameKey:
            {
                return {
                    ...state,
                    username: action.value,
                }
            }
        // username
        case setDepartmentKey:
            {
                return {
                    ...state,
                    current_department: action.value,
                }
            }
        case logout:
            {
                return {
                    ...state,
                    token: action.value,
                    username: action.value
                }
            }
        default:
            return state;
    }
}

export default configReducer;